// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt
/* eslint-disable */

frappe.query_reports["SRM Sample Request"] = {
	"filters": [
		{
			"fieldname": "priority",
			"fieldtype": "Select",
			"label": "优先级",
			"options": ['', '急','较急','一般'],
			"default": '',
			"width": 0
		},
		{
			"fieldname": "workflow_state",
			"fieldtype": "Select",
			"label": "工作流状态",
			"options": ['', 'Save','Hand In','Be Received','Rejected','Return From Purchase','Finish','Cancelled'],
			"default": '',
			"width": 0
		},
		{
			"fieldname": "creation_from",
			"fieldtype": "Date",
			"label": "制单日期从",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), -100),
			"width": 0
		},
		{
			"fieldname": "creation_to",
			"fieldtype": "Date",
			"label": "制单日期到",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), 1),
			"width": 0
		},
	],

	onload: function(report) {
		report.page.add_inner_button(__("新增打样申请"), function() {
			frappe.new_doc("SRM Sample Request");
		}).addClass('btn-primary');
		// 默认划动右侧滚动条: -901这个距离
		// setInterval(function() {
		// 	window.scrollTo(0, document.body.scrollHeight - 901);
		// }, 500)
		// console.log(report);
	},

	"formatter": function(value, row, column, data, default_formatter) {
		
		value = default_formatter(value, row, column, data);
		let format_fields = ["workflow_state"];
		// if (in_list(format_fields, column.fieldname) && data && data["workflow_state"]) {
		if (format_fields && data && data["workflow_state"]) {
			
			var color_field = data["workflow_state"];
			let color = undefined;	
			if (color_field == '完成') {
				color = 'green';
			}
			if (color_field == '拒绝') {
				color = 'red';
			}
			if (color_field == '保存') {
				color = 'blue';
			}
			if (color_field == '提交') {
				color = 'orange';
			}
			if (color){
				value =`<div style='margin:0px;padding-left:5px;color:${color}!important;'>${value}</div>`            
			}
		}

		return value;
	},

	get_datatable_options(options) {
        return Object.assign(options, {
            checkboxColumn: true,
			events: {
				onCheckRow: function(data) {
					// var select_row = []
					data[18].content = 'Y'
					console.log(data[2].content)
				},

				onSwitchColumn: function(data) {
					console.log('SwitchColumn')
				},
			}
        });
    },

	after_datatable_render: function(datatable_obj) {
		setTimeout(() => {
			// 设置高度35(默认写死了40)
			frappe.query_report.datatable.style.setStyle(`.dt-scrollable`, {height: '35vw'});
			// $('.btn-sm').hide();
		}, 60);
		// $(datatable_obj.wrapper).find(".dt-row-0").find('input[type=checkbox]').click();
	},

};




// report.page.add_inner_button(__("Total"), function() {
// 	//var filters = report.get_filter_values();		
// 	console.log(report.datatable.getColumns());	
// 	let data = report.data;			
// 	const rows = report.datatable.datamanager._filteredRows;			
// 	if (rows) {
// 			frappe.show_alert({ message:__(`${rows.length} Records`),
// 								indicator:'green'}, 5)
// 	}
// });